<?php

/**
 * @author      :   DungCV
 * @name        :   My_Plugin_Env
 * @version     :   1.0
 * @copyright   :   FPT Online
 * @todo        :   
 */
class My_Plugin_Env extends Zend_Controller_Plugin_Abstract {

    //Debug mode
    private $debug = 0;
    //Xhp mode
    private $xhp = 0;

    /**
     * Called before Zend_Controller_Front calls on the router
     * to evaluate the request against the registered routes.
     * @param Zend_Controller_Request_Abstract $request
     */
    public function routeStartup(Zend_Controller_Request_Abstract $request) {
        //Get debug
        $this->debug = $request->getParam('debug');
        $this->xhp = $request->getParam('xhp');

        //Check xhp
        if ($this->xhp) {
            xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
        }
    }

    /**
     * Called after the router finishes routing the request.
     * @param Zend_Controller_Request_Abstract $request
     */
    public function routeShutdown(Zend_Controller_Request_Abstract $request) {
        
    }

    /**
     * Called before Zend_Controller_Front enters its dispatch loop.
     * @param Zend_Controller_Request_Abstract $request
     */
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
        //Get module
        $module = strtolower($request->getParam('module', 'frontend'));

        //Layout setup
        $layoutInstance = Zend_Layout::startMvc(
                        array(
                            'layout' => 'layout',
                            'layoutPath' => APPLICATION_PATH . '/layouts/scripts',
                            'contentKey' => 'content'
                        )
        );

        //Set static content
        $appConfig = My_Global::getApplicationIni();
        $layoutInstance->appConfig = $appConfig->app;

        //Cleanup
        unset($module, $layoutInstance, $appConfig);
    }

    /**
     * Called before an action is dispatched by the dispatcher.
     * This callback allows for proxy or filter behavior.
     * By altering the request and resetting its dispatched flag
     * via Zend_Controller_Request_Abstract::setDispatched(false),
     * the current action may be skipped and/or replaced.
     * @param Zend_Controller_Request_Abstract $request
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        $module = strtolower($request->getParam('module', 'frontend'));
        if ($module == 'frontend') {
            $session = new Zend_Session_Namespace('commonSession');
            if (!$request->getParam('city_id')) {
                $request->setParam('city_id', isset($session->currentCityID) ? $session->currentCityID : 29);
            } else {
                $session->currentCityID = $request->getParam('city_id');
            }
        }
    }

    /**
     * Called after an action is dispatched by the dispatcher.
     * This callback allows for proxy or filter behavior.
     * By altering the request and resetting its dispatched flag
     * via Zend_Controller_Request_Abstract::setDispatched(false)),
     * a new action may be specified for dispatching.
     * @param Zend_Controller_Request_Abstract $request
     */
    public function postDispatch(Zend_Controller_Request_Abstract $request) {
        
    }

    /**
     * Called after Zend_Controller_Front exits its dispatch loop.
     */
    public function dispatchLoopShutdown() {
        //Check debug
        if ($this->debug) {
            //Get application configuration
            $configuration = Zend_Registry::get(APP_CONFIG);
            $configuration = $configuration->caching->toArray();

            //Check towlevels
            $arrConfiguration = array();
            if (strtolower($configuration['adapter']) == 'twolevels') {
                $arrConfiguration[] = $configuration[$configuration['adapter']]['fast_backend']['profiler'];
                $arrConfiguration[] = $configuration[$configuration['adapter']]['slow_backend']['profiler'];
            } else {
                $arrConfiguration[] = $configuration['profiler'];
            }

            //Loop to get data            
            $stringAppend = '';
            foreach ($arrConfiguration as $profilerConfiguration) {
                //Get profiler
                $profiler = Fpt_Cache::getInstance($profilerConfiguration);

                //Add body               
                $stringAppend .= $profiler->getProfilerData();
            }

            //Append body
            $this->getResponse()->appendBody($stringAppend);
        }

        //Check xhp
        if ($this->xhp) {
            //Get debug data xhprof
            $xhprof_data = xhprof_disable();
            $stringAppend = Fpt_Utility::getXhp($xhprof_data);

            //Append body
            $this->getResponse()->appendBody($stringAppend);
        }

        //Strip output buffer
        $output = $this->getResponse()->getBody();
        //$output = My_Filter::stripBuffer($output);
        $output = $output;

        //Set body
        $this->getResponse()->setBody($output);

        //Sent header
        $this->getResponse()->setHeader('X-UA-Compatible', 'IE=EmulateIE7');
    }

}